package com.wejoy.video.middle.station.data.service.system.impl;

import com.wejoy.video.middle.station.data.domain.system.SysDept;
import com.wejoy.video.middle.station.data.domain.system.SysDeptUser;
import com.wejoy.video.middle.station.data.domain.system.SysUser;
import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.mapper.system.ISysDeptUserMapper;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.system.ISysDeptService;
import com.wejoy.video.middle.station.data.service.system.ISysDeptUserService;
import com.wejoy.video.middle.station.data.service.system.ISysUserService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import com.wejoy.video.middle.station.data.vo.system.SysDeptUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 系统-部门用户
 *
 * @author admin
 * @date 2021年11月16日 19:40:45
 */
@Service
public class SysDeptUserServiceImpl implements ISysDeptUserService {
    @Autowired
    private ISysDeptUserMapper sysDeptUserMapper;
    @Autowired
    private IIdWorkerService idWorkerService;
    @Autowired
    private ISysDeptService sysDeptService;

    @Override
    public void save(SysDeptUser req, String operator) {
        req.setOperator(operator);
        if (null == req.getId()) {
            insertSelective(req);
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(SysDeptUser req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return sysDeptUserMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(SysDeptUser sysDeptUser) {
        WJAssert.notNull(sysDeptUser, "入参对象不能为空");
        WJAssert.notNull(sysDeptUser.getId(), "id参数错误");
        SysDeptUser where = new SysDeptUser();
        where.setId(sysDeptUser.getId());
        return sysDeptUserMapper.updateSelective(sysDeptUser, where);
    }

    @Override
    public int getCount(SysDeptUser sysDeptUser) {
        checkStringLength(sysDeptUser, true);
        return sysDeptUserMapper.getCount(sysDeptUser);
    }

    @Override
    public SysDeptUser getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        SysDeptUser where = new SysDeptUser();
        where.setId(id);
        return sysDeptUserMapper.getOne(where);
    }

    @Override
    public List<SysDeptUser> getList(SysDeptUser sysDeptUser) {
        checkStringLength(sysDeptUser, true);
        return sysDeptUserMapper.getList(sysDeptUser);
    }

    @Override
    public List<SysDeptUser> getPageList(SysDeptUser sysDeptUser) {
        checkStringLength(sysDeptUser, true);
        return sysDeptUserMapper.getPageList(sysDeptUser);
    }

    @Override
    public void saveUserDept(SysUser user, String operator) {
        SysDeptUser req = new SysDeptUser();
        req.setUserId(user.getId());
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        SysDeptUser where = new SysDeptUser();
        where.setUserId(user.getId());
        sysDeptUserMapper.updateSelective(req, where);

        user.getDeptIdList().forEach(item -> {
            SysDept sysDeptDB = sysDeptService.getOneById(item);
            SysDeptUser data = new SysDeptUser();
            data.setDeptId(sysDeptDB.getId());
//            data.setDeptName(sysDeptDB.getName());
            data.setUserId(user.getId());
            data.setUserName(user.getName());
            data.setOperator(operator);
            data.setStatus(EnumStatus.DEL.getCode());
            save(data, operator);
        });
    }

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public Map<String, Object> getDeptUserListAndUserList(Long deptId) {
        Map<String, Object> result = new HashMap<>(2);
        SysDeptUser where = new SysDeptUser();
        where.setDeptId(deptId);
        List<Long> userIdList = getList(where).stream().map(item -> {
            return item.getUserId();
        }).collect(Collectors.toList());
        result.put("deptUserList", userIdList);
        List<SysDeptUserVO> userList = sysUserService.getList(new SysUser()).parallelStream().map(item -> {
            return new SysDeptUserVO(item.getId(), item.getName());
        }).collect(Collectors.toList());
        result.put("userList", userList);
        return result;
    }

    @Override
    public List<SysDeptUser> getUserDeptListByUserId(Long userId) {
        SysDeptUser sysDeptUser = new SysDeptUser();
        sysDeptUser.setUserId(userId);
        return getList(sysDeptUser);
    }

    @Override
    public void audit(Long id, String operator) {
        SysDeptUser req = new SysDeptUser();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        SysDeptUser req = new SysDeptUser();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        SysDeptUser req = new SysDeptUser();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    @Override
    public List<SysDeptUserVO> getDepUser(Long depId) {
        return sysDeptUserMapper.getDepUser(depId);
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(SysDeptUser sysDeptUser, boolean nullAble) {
        WJAssert.notNull(sysDeptUser, "入参对象不能为空");
        WJAssert.limitMaxLength(sysDeptUser.getOperator(), nullAble, 100, "操作者不合法!");
        WJAssert.limitMaxLength(sysDeptUser.getRemark(), true, 255, "备注不合法!");
    }
}
